﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 18.03.2008
 * Zeit: 23:10
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;

namespace Gamadus.Designer
{
	/// <summary>
	/// Description of CodeEditor.
	/// </summary>
	public partial class CodeEditor : Form, IEditWindow
	{
		Action action;
		public CodeEditor(MainForm form, Action action)
		{
			this.action = action;
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			textEditorControl.Document.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter("C#");

			this.MdiParent = form;
			
			textEditorControl.Text = string.Format("{0}", action.ParamDisplayValues);
		}
		
		public Resource Editing
		{
			get
			{
				return null;
			}
		}
		
		void ToolStripButtonOKClick(object sender, EventArgs e)
		{
			this.Close(false);
		}
		
		bool closeDontAsk = false;
		
		public void Close(bool askForSave)
		{
			closeDontAsk = !askForSave;
			this.Close();
		}
		
		void CodeEditorFormClosing(object sender, FormClosingEventArgs e)
		{
			bool save = true;
			if(!closeDontAsk)
			{
				DialogResult result = MessageBox.Show("Save changes to the code?","Warning",
				                                      MessageBoxButtons.YesNoCancel,
				                                      MessageBoxIcon.Warning);
				if(result == DialogResult.Cancel)
				{
					e.Cancel = true;
					return;
				}
				if(result == DialogResult.No)
					save = false;
			}
			closeDontAsk = false;
			
			if(save)
			{
				ActionParamString param = action.ParamList["Code"] as ActionParamString;
				param.Value = textEditorControl.Text;
			}
			
			ResourceMgr.Instance.editWindows.Remove(this);
			if(save)
				ResourceMgr.Instance.DidChanges = true;
		}
		
		public void UpdateEditForm()
		{
			//textBoxName.Text = res.Name;
		}
	}
}
